/**
 * Copyright (c) 2013-2015 www.javahih.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.hihsoft.ui_widget.datagrid;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hihframework.core.utils.StringHelpers;
import com.hihframework.exception.ControllerException;
import com.hihframework.osplugins.json.JsonUtil;
import com.hihsoft.baseclass.web.controller.javahihBaseController;
import com.hihsoft.sso.business.service.TsysFlatService;

/**
 * <p> Title:使用优秀的开源列表组件flexigrid演示 </p>
 * <p> Description:主要以子系统列表为功能演示</p>
 * <p> Copyright: Copyright (c) 2013 </p>
 * <p> Company:hihsoft.co.,ltd </p>
 *
 * @author hihsoft.co.,ltd
 * @version 1.0
 */
@Controller
@RequestMapping("/uiDataGridDemoController.do")
public class UI_DatagridDemoController extends javahihBaseController {
	@Autowired
	private TsysFlatService tsysFlatService;
	
	/**
	* 访问列表
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	@RequestMapping(params="method=list")
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws ControllerException {
		ModelAndView mv = new ModelAndView("/ui_widget/ui_datagrid_demo");
		boolean flag=true;
		String orders = getParam(request, "orders");
		String flatname=getParam(request, "srh_flatname");
		String json="";
		if(StringHelpers.isNull(orders)){
			orders=" flatdesc ";
		}else{
			orders=orders.replace("flatname", "flatname");
		}
		addOrders(request, orders);
		String r = request.getParameter("rows");
		String p = request.getParameter("page");
		int rows = r == null ? 15 : Integer.parseInt(r);
		int page = p == null ? 1 : Integer.parseInt(p);
		int total=tsysFlatService.getFlatsList(flatname,orders, 0, 0).size();
		int pages = (total % rows == 0) ? total / rows : total / rows + 1;
		if (page > pages) page = 1;
		List<Map<String,Object>> map=tsysFlatService.getFlatsList(flatname,orders, rows, page);
		json = "{\"total\": " + total + ", \"rows\": "
				+ JsonUtil.toString(map, JsonUtil.COLLECTION_FILTER) + "}";
		renderJson(response, json);
	    return null;
	}

}

